package cn.iocoder.yudao.module.common.controller.admin.gradesubjecttextbookversion;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.common.controller.admin.gradesubjecttextbookversion.vo.*;
import cn.iocoder.yudao.module.common.dal.dataobject.gradesubjecttextbookversion.GradeSubjectTextbookVersionDO;
import cn.iocoder.yudao.module.common.service.gradesubjecttextbookversion.GradeSubjectTextbookVersionService;

@Tag(name = "管理后台 - 年级学科版本")
@RestController
@RequestMapping("/common/grade-subject-textbook-version")
@Validated
public class GradeSubjectTextbookVersionController {

    @Resource
    private GradeSubjectTextbookVersionService gradeSubjectTextbookVersionService;

    @PostMapping("/create")
    @Operation(summary = "创建年级学科版本")
    @PreAuthorize("@ss.hasPermission('common:grade-subject-textbook-version:create')")
    public CommonResult<Long> createGradeSubjectTextbookVersion(@Valid @RequestBody GradeSubjectTextbookVersionSaveReqVO createReqVO) {
        return success(gradeSubjectTextbookVersionService.createGradeSubjectTextbookVersion(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新年级学科版本")
    @PreAuthorize("@ss.hasPermission('common:grade-subject-textbook-version:update')")
    public CommonResult<Boolean> updateGradeSubjectTextbookVersion(@Valid @RequestBody GradeSubjectTextbookVersionSaveReqVO updateReqVO) {
        gradeSubjectTextbookVersionService.updateGradeSubjectTextbookVersion(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除年级学科版本")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('common:grade-subject-textbook-version:delete')")
    public CommonResult<Boolean> deleteGradeSubjectTextbookVersion(@RequestParam("id") Long id) {
        gradeSubjectTextbookVersionService.deleteGradeSubjectTextbookVersion(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得年级学科版本")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('common:grade-subject-textbook-version:query')")
    public CommonResult<GradeSubjectTextbookVersionRespVO> getGradeSubjectTextbookVersion(@RequestParam("id") Long id) {
        GradeSubjectTextbookVersionDO gradeSubjectTextbookVersion = gradeSubjectTextbookVersionService.getGradeSubjectTextbookVersion(id);
        return success(BeanUtils.toBean(gradeSubjectTextbookVersion, GradeSubjectTextbookVersionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得年级学科版本分页")
    @PreAuthorize("@ss.hasPermission('common:grade-subject-textbook-version:query')")
    public CommonResult<PageResult<GradeSubjectTextbookVersionRespVO>> getGradeSubjectTextbookVersionPage(@Valid GradeSubjectTextbookVersionPageReqVO pageReqVO) {
        PageResult<GradeSubjectTextbookVersionDO> pageResult = gradeSubjectTextbookVersionService.getGradeSubjectTextbookVersionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GradeSubjectTextbookVersionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出年级学科版本 Excel")
    @PreAuthorize("@ss.hasPermission('common:grade-subject-textbook-version:export')")
    @OperateLog(type = EXPORT)
    public void exportGradeSubjectTextbookVersionExcel(@Valid GradeSubjectTextbookVersionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GradeSubjectTextbookVersionDO> list = gradeSubjectTextbookVersionService.getGradeSubjectTextbookVersionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "年级学科版本.xls", "数据", GradeSubjectTextbookVersionRespVO.class,
                        BeanUtils.toBean(list, GradeSubjectTextbookVersionRespVO.class));
    }

}